

const db = require("../db/index")
const plugins = require("../utils/index");

// const express = require('express')
// const app  = express()
// const port = 3000

// let server = app

// 上传商品的数据
pushShop = (req, res) => {
    // console.log(req.query)

    let cakeShopobj = req.query
    let sql = "insert into category2 set ?"
    let p = new Promise((resolve, reject) => {
        db.query(sql, cakeShopobj, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(result)
        })
    })

    p.then(r => {
        res.send({
            code: 200,
            message: '成功',
        })
    }, v => {
        res.send({
            code: 401,
            message: v,
        })
    })
}

//获取商品的数据
getShop = (req, res) => {

    let storeId = req.query.storeId
    let openId = req.query.openId
    let sql = "select * from cartlist where storeId = ? and openId=?"

    let p1 = new Promise((resolve, reject) => {

        db.query(sql, [storeId, openId], (err, result) => {

            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return resolve(result)
        })

    })


    let p2 = new Promise((resolve, reject) => {

        let sql = "select * from category2"

        db.query(sql, "", (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                resolve(result)
            }
        })

    })

    let p3 = new Promise((resolve, reject) => {

        let sql = "select * from category1"

        db.query(sql, "", (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                resolve(result)
            }
        })

    })

    let result = Promise.all([p1, p2,p3])

    result.then(r => {
        r[2].forEach(current=>{
            current.children=[]
            r[1].forEach((item) => {
                r[0].forEach(info => {
                    if (item.cat_id == info.cat_id) {
                        item.num = info.num
                        item.isShow = info.isShow
                    }
                })
               if(current.cat_pid == item.cat_pid){
                    current.children.push(item)
               }

            })
        })


        res.send(
            {
                status: 200,
                data: r[2],
                mess: "获取数据成功"
            }
        )
    }, e => {
        res.send(
            {
                status: 400,
                data: e,
                mess: "获取数据失败"
            }
        )
       
    })


}

// 获取纯商品数据
getShopInfo = (req,res)=>{
    let p2 = new Promise((resolve, reject) => {

        let sql = "select * from category2 "

        db.query(sql, "", (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                resolve([])
            }
        })

    })
    p2.then(r=>{
        res.send({
            status:200,
            data:r,
            mess:"获取数据成功"
        })
    },e=>{
        res.send({
            status:200,
            data:[],
            mess:e
        })
    })
}

// 更改商品数据
changeShop = (req,res)=>{
    // console.log(req.query)
    let data = req.query
    let cat_id = req.query.cat_id
    let sql ="update category2 set ? where cat_id = ? "
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[data,cat_id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("修改成功")
            }else{
                reject("修改失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
},

//删除商品
deleteShop =(req,res)=>{
    let id = req.query.cat_id
    let sql ="delete from category2  where cat_id = ?"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("删除成功")
            }else{
                reject("删除失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
},

//获取商品标题
getShopTitle = (req, res) => {
    let sql = "select * from  category1"

    let p = new Promise((resolve, reject) => {
        db.query(sql, '', (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) return resolve(result)
        })
    })

    p.then((success) => {
        res.send({
            code: 200,
            data: success,
            mess: '成功'
        })
    }, (err) => {
        res.send({
            code: 401,
            data: err,
            mess: "失败"
        })
    })

}

//上传商品的标题

pushShopTitle = (req, res) => {

    let sql = "insert into category1 set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, req.query, (err, result) => {
            if (err) return reject(err)

            if (result.affectedRows > 0) return resolve(result)
        })
    })

    p.then((success) => {
        res.send({
            code: 200,
            data: success,
            mess: '成功'
        })
    }, (err) => {
        res.send({
            code: 401,
            data: err,
            mess: "失败"
        })
    })

}
// 删除商品标题
deleteShopTitle =(req,res)=>{
    let id = req.query.cat_id
    let sql ="delete from category1  where cat_id = ?"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("删除成功")
            }else{
                reject("删除失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
},

// 编辑商品标题
setShopTitle =(req,res)=>{
    let data = req.query
    let cat_id = req.query.cat_id
    let sql ="update category1 set ? where cat_id = ? "
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[data,cat_id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("修改成功")
            }else{
                reject("修改失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
}

// 上传购物车的数据
pushShoppingCart = (req, res) => {

    let cakeShopobj = req.query
    let sql = "insert into shoppingcart set ?"
    let p = new Promise((resolve, reject) => {
        db.query(sql, cakeShopobj, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(result)
        })
    })

    p.then(r => {
        res.send({
            code: 200,
            message: '成功',
        })
    }, v => {
        res.send({
            code: 401,
            message: v,
        })
    })
}


// 获取购物车的数据
getShoppingCartData = (req, res) => {

    let sql = "select * from shoppingcart";

    let p = new Promise((resolve, reject) => {
        db.query(sql, "", (err, result) => {
            if (err) return reject("输入的信息有误")
            if (result.length > 0) {
                resolve(result)
            } else {
                reject("获取数据失败")
            }
        })
    })

    p.then(r => {
        res.send({
            code: 200,
            mess: '成功',
            data: r
        })
    }, v => {
        res.send({
            code: 401,
            mess: r
        })
    })

}

//获取商店地址
getShopAddress = (req, res) => {
    let sql = "select * from address order by id desc";

    let p = new Promise((resolve, reject) => {
        db.query(sql, "", (err, result) => {
            if (err) return reject("输入的信息有误")
            if (result.length > 0) {
                
                //    console.log(result)
                resolve(result)
            } else {
                reject("获取数据失败")
            }
        })
    })

    p.then(r => {
        res.send({
            code: 200,
            mess: '成功',
            data: r
        })
    }, v => {
        res.send({
            code: 401,
            mess: r
        })
    })
}

// 编辑店铺
editStoreAddress = (req,res)=>{
    let data = req.query
    let id = req.query.id
    let sql ="update address set ? where id = ?"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[data,id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("修改成功")
            }else{
                reject("修改失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
}

// 删除店铺
deleteStoreAddress = (req,res)=>{
    let id = req.query.id
    let sql ="delete from address  where id = ?"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[id],(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows>0){
                resolve("删除成功")
            }else{
                reject("删除失败")
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:1,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            data:0,
            mess:e
        })
    })
}

//插入商店的地址
pushShopAddress = (req, res) => {

    let sql = "insert into address set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, req.query, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(result)
        })
    })

    p.then((r) => {
        res.send({
            status: 200,
            mess: "成功"
        })
    }, e => {
        res.send({
            status: 401,
            mess: e
        })
    })


}


// 插入购物车
pushCartList = (req, res) => {

    // console.log(req.query)

    let dataObj = req.query

    let sql = "insert into cartlist set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, dataObj, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(result)
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "成功",

        })
    }, e => {
        res.send({
            status: 401,
            mess: e,
        })
    })
    // res.send({
    //     status:200,
    //     mess:"成功",
    //     data:req.query       
    // })

}

//获取购物车的数据

getCartList = (req, res) => {

    let storeId = req.query.storeId
    let openId = req.query.openId
    let sql = "select * from cartlist where storeId = ? and openId=?"

    let p = new Promise((resolve, reject) => {

        db.query(sql, [storeId, openId], (err, result) => {
            // console.log("----------------------")
            // console.log(result)
            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return resolve(result)
        })

    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "成功",
            data: r
        })
    }, e => {
        res.send({
            status: 401,
            mess: e,
        })
    })
}

//更改购物车的数量
setShopNum = (req, res) => {


    let sql = "update  cartlist set num =? WHERE id = ? and openId=?"
    let openId = req.query.openId
    // 购物车列表的id
    let id = req.query.id
    // 购物车列表的num
    let num = req.query.num
    let p = new Promise((resolve, reject) => {
        db.query(sql, [num, id, openId], (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(result)
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "修改成功"
        })
    }, e => {
        res.send({
            status: 401,
            mess: e
        })
    })

}

//从购物车中删除

deleteCartShop = (req, res) => {
    let id = req.query.id
    let openId = req.query.openId
    let sql = "DELETE from cartlist where id = ? and openId = ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [id, openId], (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows == 1) return resolve(resolve)
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "成功",
        })
    }, e => {
        res.send({
            status: 401,
            mess: e,
        })
    })
}

//清除购物车

clearCart = (req, res) => {
    let storeId = req.query.storeId
    let openId = req.query.openId
    let sql = "delete from cartlist where storeId = ? and openId=?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [storeId, openId], (err, result) => {
            if (err) return reject(err)
            // console.log(result.affectedRows)
            if (result.affectedRows > 0) return resolve(resolve)
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "成功",
        })
    }, e => {
        res.send({
            status: 401,
            mess: e,
        })
    })
}


// 提交订单

pushOrder = (req, res) => {
    // console.log(req.query)
    let data = req.query
    let sql = "insert into orderlist set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, data, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) return resolve()
        })
    })
    p.then(r => {
        res.send({
            status: 200,
            mess: "提交订单成功"
        })
    }, e => {
        res.send({
            status: 401,
            mess: "提交订单失败"
        })
    })

}

// 后台获取订单数据，提交订单的

cmsGetOrderList = (req,res)=>{
    let sql = "select * from orderlist,address where  orderlist.storeId=address.id and orderlist.active=1   order by orderDate desc"
    let sql2 = "select * from discount"

    let p1 = new Promise((resolve,reject)=>{
        db.query(sql,'',(err,result)=>{
            //   console.log(result)
            if(err) return reject(err)
            if(result.length>0){
                return resolve(result)
            }else{
                // console.log("aaa")
                return resolve([])
            }
        })
    })
    let p2 = new Promise((resolve,reject)=>{
        db.query(sql2,'',(err,result)=>{
            
            if(err) return reject(err)
            if(result.length>0){
                return resolve(result)
            }else{
                return resolve([])
            }
        })
    })
    let p = Promise.all([p1,p2])
    p.then(r=>{
      let newArr=  r[0].map(item=>{
        item.orderList = JSON.parse(item.orderList)
        item.orderDate = plugins.dateConversion(item.orderDate)
            r[1].forEach(info=>{
                if(item.disId==info.disId){
                    item = {...item,...info}
                    // console.log(item)
                }
            })
            return item
        })

        res.send({
            status:200,
            data:newArr,
            mess:"获取数据成功"
        })
    },e=>{
        res.send({
            status:400,
            data:[],
            mess:"获取数据失败"
        })
    })

},


// 后台获取订单数据，商家接收的订单

receivedOrderList = (req,res)=>{
    let sql = "select * from orderlist,address where  orderlist.storeId=address.id and orderlist.active=2   order by orderDate desc"
    let sql2 = "select * from discount"

    let p1 = new Promise((resolve,reject)=>{
        db.query(sql,'',(err,result)=>{
            //   console.log(result)
            if(err) return reject(err)
            if(result.length>0){
                return resolve(result)
            }else{
                // console.log("aaa")
                return resolve([])
            }
        })
    })
    let p2 = new Promise((resolve,reject)=>{
        db.query(sql2,'',(err,result)=>{
            
            if(err) return reject(err)
            if(result.length>0){
                return resolve(result)
            }else{
                return resolve([])
            }
        })
    })
    let p = Promise.all([p1,p2])
    p.then(r=>{
      let newArr=  r[0].map(item=>{
            item.orderList = JSON.parse(item.orderList)
            item.orderDate = plugins.dateConversion(item.orderDate)
            r[1].forEach(info=>{
                if(item.disId==info.disId){
                    item = {...item,...info}
                    // console.log(item)
                }
            })
            return item
        })

        res.send({
            status:200,
            data:newArr,
            mess:"获取数据成功"
        })
    },e=>{
        res.send({
            status:400,
            data:[],
            mess:"获取数据失败"
        })
    })


},
// 获取订单的数据
getOrderList = (req, res) => {
    let openId = req.query.openId
    let type = req.query.type1
    // console.log(req.query)
    let limitNum = parseInt(req.query.limitNum)
    // console.log(limitNum)

    let sql = ""
    if (type == 0) {
        // console.log("fefes0")
        sql = "select * from orderlist,address where  openId = ? and active < 3 and orderlist.storeId=address.id   order by orderDate desc limit ?,5"
    }

    if (type == 1) {
        // console.log("fefes1")
        sql = "select * from orderlist,address where  openId = ? and active >2 and orderlist.storeId=address.id   order by orderDate desc limit ?,5"
    }
    if (type == 2) {
        // console.log("fefes2")
        sql = "select * from orderlist,address where  openId = ? and active >4 and orderlist.storeId=address.id   order by orderDate desc limit ?,5"
    }



    let p = new Promise((resolve, reject) => {

        db.query(sql, [openId, limitNum * 5], (err, result) => {
            // console.log(result,"fesfes")
            if (err) return reject(err)
            if (result.length > 0) {
                resolve(result)
            } else (
                reject("没有数据了")
            )
        })

    })

    p.then(r => {
        r.forEach(item => {
            item.orderList = JSON.parse(item.orderList)
            item.orderDate = plugins.dateConversion(item.orderDate)
        })

        return new Promise((resolve, reject) => {
            let sql = "select * from discount where disId=?"
            r.map((item, index) => {

                db.query(sql, item.disId, (err, result) => {
                    // console.log(result)
                    if (err) return reject(err)
                    item.shopDiscount = result

                    if (index == r.length - 1) {
                        setTimeout(() => {
                            resolve(r)
                        }, 300)
                    }
                })
            })
        })

    }, e => {
        // console.log("第一层")
        res.send({
            status: 400,
            data: [],
            mess: e

        })
        return new Promise((resolve, reject) => { })
    }).then(rr => {
        // console.log("第二层")
        // console.log(rr)
        res.send({
            status: 200,
            data: rr,
            mess: "获取数据成功"

        })
    }, ee => {
        // console.log("第三层")
        res.send({
            status: 400,
            data: [],
            mess: "获取数据失败"

        })
    })



}

// 获取一条订单数据
getOneOrder = (req, res) => {
    let openId = req.query.openId
    let orderId = req.query.orderId
    let orderNo = req.query.orderNo

    // let sql = "select * from orderList where openId = ? order by orderId desc"
    let sql = "select * from orderlist,address where  openId = ? and orderNo = ? and orderlist.storeId=address.id   order by orderDate desc"

    let p = new Promise((resolve, reject) => {

        db.query(sql, [openId, orderNo], (err, result) => {
            if (err) return reject(err)
            if (result.length > 0) {
                resolve(result)
            } else (
                reject("获取数据失败")
            )
        })

    })

    p.then(r => {
        r.forEach(item => {
            item.orderList = JSON.parse(item.orderList)
            item.orderDate = plugins.dateConversion(item.orderDate)
        })

        return new Promise((resolve, reject) => {
            let sql = "select * from discount where disId=?"
            r.map((item, index) => {

                db.query(sql, item.disId, (err, result) => {
                    // console.log(result)
                    if (err) return reject(err)
                    item.shopDiscount = result

                    if (index == r.length - 1) {
                        setTimeout(() => {
                            resolve(r)
                        }, 300)
                    }
                })
            })
        })

    }, e => {
        res.send({
            status: 200,
            data: [],
            mess: e

        })
        return new Promise((resolve, reject) => { })
    }).then(rr => {
        res.send({
            status: 200,
            data: rr,
            mess: "获取数据成功"

        })
    }, ee => {
        res.send({
            status: 400,
            data: [],
            mess: "获取数据失败"

        })
    })



}



// 更改订单里面的active

changeOrderActive = (req, res) => {
    // console.log(req.query)
    let active = req.query.active
    let orderNo = req.query.orderNo
    let orderDate = req.query.orderDate
    // let mealCode = req.query.mealCode
    let sql = "update orderlist set active = ?, orderDate=?  where orderNo=?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [active, orderDate, orderNo], (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) return resolve("修改成功")
            if (result.affectedRows == 0) return reject("修改失败")
        })

    })

    p.then(r => {
        // console.log(r)
        res.send({
            status: 200,
            mess: r
        })
    }, e => {
        res.send({
            status: 401,
            mess: e
        })
    })

}

// 新增优惠券
addDiscount = (req, res) => {

    let data = req.query

    let sql = "insert into discount set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, data, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) return resolve("新增成功")
            if (result.affectedRows == 0) return reject("新增失败")
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            mess: r
        })
    }, e => {
        res.send({
            status: 400,
            mess: e
        })
    })

    // res.send({
    //     status:200,
    //     mess:"成功"
    // })

}

// 获取优惠券
getDiscount = (req, res) => {
    let openId = req.query.openId
  
    let sql = "select * from discount"

    let p1 = new Promise((resolve, reject) => {
        db.query(sql, "", (err, result) => {
            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return resolve([])
        })
    })

    let sql1 = "select * from discount,userdiscount where userdiscount.disId=discount.disId and openId =? "
    let p2 = new Promise((resolve, reject) => {
        db.query(sql1, openId, (err, result) => {

            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return resolve([])
        })
    })

    let p = Promise.all([p1,p2])

  
    p.then(r => {
            // console.log(r[1])
        let newArr = r[0].map(item=>{
            r[1].forEach(info=>{
                if(item.disId==info.disId){
                    // console.log(info)
                    item.disActive = info.disActive
                }
            })
            item.startDate = plugins.dateConversion(item.startDate)
            item.dealDate = plugins.dateConversion(item.dealDate)
            return item
        })


        res.send({
            status: 200,
            mess: "获取优惠卷成功",
            data: newArr
        })
    }, e => {
        res.send({
            status: 400,
            mess: e,

        })
    })

}

//用户新增的优惠券
userAddDiscount = (req, res) => {
    let data = req.query

    let sql = "insert into userdiscount set ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, data, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) {
                resolve("兑换成功")
            } else {
                reject("兑换失败")
            }
        })
    })
    p.then(r => {
        res.send({
            status: 200,
            mess: r
        })
    }, e => {
        res.send({
            status: 300,
            mess: e
        })
    })

}

// 获取用户新增的优惠券
getUserAddDiscount = (req, res) => {

    // console.log(req.query)
    let openId = req.query.openId

    let sql = "select * from discount,userdiscount where userdiscount.disId=discount.disId and openId =? "

    let p = new Promise((resolve, reject) => {
        db.query(sql, openId, (err, result) => {

            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return reject("获取数据失败")
        })
    })

    p.then(r => {
        r.forEach(item => {
            // item.disActive = 2
            item.startDate = plugins.dateConversion(item.startDate)
            item.dealDate = plugins.dateConversion(item.dealDate)
        })
        res.send({
            status: 200,
            mess: "获取优惠卷成功",
            data: r
        })
    }, e => {
        res.send({
            status: 400,
            mess: e,
            data: []

        })
    })
}

//用户提交订单时，删除用户所领的优惠券

deleteUserDiscount = (req, res) => {
    // console.log(req.query)
    let openId = req.query.openId
    let userId = req.query.userId

    let sql = "delete from userdiscount where userId = ? and openId = ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [userId, openId], (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) {
                return resolve("删除成功")
            } else {
                return reject("删除失败")
            }
        })
    })

    p.then(r => {
        // console.log("fesfsae成功")
        res.send({
            status: 200,
            mess: r
        })
    }, e => {
        // console.log("fesfsae失败")
        res.send({
            status: 200,
            mess: e
        })
    })
}

//修改用户优惠券的列表isTrue
changedisIstrue = (req, res) => {
    let sql = "update userdiscount set isTrue = 0"

    let { openId, userid, istrue } = req.query

    let p = new Promise((resolve, reject) => {
        db.query(sql, "", (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) {
                resolve("修改成功")
            } else {
                reject("修改失败")
            }
        })
    })

    p.then(succ => {

        let sql1 = "update userdiscount set isTrue = ? where openId = ? and userId=?"
        // console.log(istrue,openId,userid)
        return new Promise((resolve, reject) => {
            db.query(sql1, [istrue, openId, userid], (e, r) => {
                if (e) return reject(e)

                if (r.affectedRows > 0) {
                    resolve("更新成功")
                } else {
                    reject("更新失败")
                }
            })
        })
    }, err => {

        res.send({
            status: 400,
            mess: err
        })
        return new Promise((resolve, reject) => { })
    }).then(r => {
        res.send({
            status: 200,
            mess: r
        })
    }, e => {
        res.send({
            status: 400,
            mess: e
        })

    })

}

// 看看用户有没有支付的密码

getUerPayPassword = (req, res) => {
    let openId = req.query.openId

    // console.log(req.query)

    let sql = "select * from paypassword where openId=?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, openId, (err, result) => {
            // console.log(result)
            if (err) return reject(0)
            if (result.length > 0) {
                resolve(1)
            } else {
                reject(0)
            }
        })
    })
    p.then(r => {
        res.send({
            status: 200,
            data: r,
            mess: "有密码喔"
        })
    }, e => {
        res.send({
            status: 400,
            data: e,
            mess: "没有密码喔"
        })
    })

}


// 插入用户支付密码
insertPaypassword = (req, res) => {
    // let openId = req.query.openId
    // let password = req.query.password
    let data = req.query
    let sql = "insert into paypassword set ?"

    let p = new Promise((resolve, reject) => {

        db.query(sql, data, (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) {
                resolve("插入成功")
            } else {
                resolve("插入失败")
            }
        })

    })
    p.then(r => {
        res.send({
            status: 200,
            data: 1,
            mess: r
        })
    }, e => {
        res.send({
            status: 400,
            data: 0,
            mess: e
        })
    })
}

// 获取用户的钱

getUserMoney = (req, res) => {
    let openId = req.query.openId

    let sql = "select * from money where openId = ?"

    let p = new Promise((resolve, reject) => {

        db.query(sql, openId, (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                reject("用户还没钱呢")
            }
        })

    })

    p.then(r => {
        r.forEach(item => {
            item.mDate = plugins.dateConversion(item.mDate)
        })
        res.send({
            status: 200,
            data: r,
            mess: "获取到钱了呢"
        })
    }, e => {
        res.send({
            status: 400,
            data: [],
            mess: e
        })
    })
}

// 匹配用户的支付密码 和插入金额

matchPayPassword = (req, res) => {
    // console.log(req.query)
    let password = req.query.password
    let openId = req.query.openId
    let money = req.query.money
    let active = 1

    let mDate = plugins.dateConversion(new Date())

    let sql = "select * from paypassword where password= ? and openId = ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [password, openId], (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(1)
            } else {
                reject(0)
            }
        })
    })
    p.then(r => {

        let sql = " insert into money set openId = ?,money=?,active=?,mDate=?"

        return new Promise((resolve, reject) => {
            db.query(sql, [openId, money, active, mDate], (err, succ) => {
                if (err) return reject(err)

                if (succ.affectedRows > 0) {
                    resolve("充值成功")
                } else {
                    reject("充值失败")
                }
            })
        })


    }, e => {
        res.send({
            status: 400,
            data: 0,
            mess: "密码匹对失败"
        })
        return new Promise((resolve, reject) => { })
    }).then(rr => {
        res.send({
            status: 200,
            mess: rr,
            data: 1
        })
    }, ee => {
        res.send({
            status: 400,
            mess: ee,
            data: 2
        })
    })

}

//不匹配密码插入金额
noMatchPassword = (req, res) => {

    let openId = req.query.openId
    let money = req.query.money
    let mDate = plugins.dateConversion(new Date())
    let active = 1

    let sql = " insert into money set openId = ?,money=?,active=?,mDate=?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [openId, money, active, mDate], (err, succ) => {
            if (err) return reject(err)

            if (succ.affectedRows > 0) {
                resolve("充值成功")
            } else {
                reject("充值失败")
            }
        })
    })
    p.then(r => {
        res.send({
            status: 200,
            data: 1,
            mess: "插入成功"
        })
    }, e => {
        res.send({
            status: 400,
            data: 0,
            mess: "插入失败"
        })
    })

}

// 获取用户的充值信息

getTransactionInfo = (req, res) => {
    // console.log(req.query.limitNum)
    let openId = req.query.openId
    let limitNum = parseInt(req.query.limitNum)
    let sql = "select * from money where openId = ? and active = 1 order by mDate desc LIMIT ?,3 "

    let p = new Promise((resolve, reject) => {
        db.query(sql, [openId, limitNum * 3], (err, result) => {
            //   console.log(result)
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                resolve([])
            }
        })
    })
    p.then(r => {
        r.forEach(item => {
            item.mDate = plugins.dateConversion(item.mDate)
        })
        res.send({
            status: 200,
            data: r,
            mess: "获取成功"
        })
    }, e => {
        res.send({
            status: 400,
            data: [],
            mess: e
        })
    })

}

// 匹配支付密码
matchPassword = (req, res) => {
    let password = req.query.password
    let openId = req.query.openId

    let sql = "select * from paypassword where password= ? and openId = ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [password, openId], (err, result) => {
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(1)
            } else {
                reject(0)
            }
        })
    })

    p.then(r => {
        res.send({
            status: 200,
            data: 1,
            mess: "密码正确"
        })
    }, e => {
        res.send({
            status: 400,
            data: 0,
            mess: "密码错误"
        })
    })
}

// 更改交易密码
changePayPassword = (req, res) => {
    let openId = req.query.openId
    let password = req.query.password

    let sql = "update paypassword set password = ? where openId = ?"

    let p = new Promise((resolve, reject) => {
        db.query(sql, [password, openId], (err, result) => {
            if (err) return reject(err)
            if (result.affectedRows > 0) {
                resolve("更新成功")
            } else {
                reject("更新失败")
            }
        })
    }).then(r => {
        res.send({
            status: 200,
            mess: r,
            data: 1
        })
    }, e => {
        res.send({
            status: 400,
            mess: e,
            data: 0
        })
    })
}

// 插入奖品池
insertLuckDraw =(req,res)=>{
    let data = req.query
    
    let sql = "insert into luckdraw set ? "
    let p = new Promise((resolve,reject)=>{
        db.query(sql,data,(err,result)=>{
            if(err) return reject(0)
            if(result.affectedRows>=0){
                resolve(1)
            }else{
                resolve(1)
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            mess:"成功",
            data:r
        })
    },e=>{
        res.send({
            status:400,
            mess:"失败",
            data:e
        })
    })

}

// 获取奖池数据
getLuckDraw=(req,res)=>{
    

    let sql = "select * from luckdraw "

    let p = new Promise((resolve, reject) => {

        db.query(sql,'', (err, result) => {
 
            if (err) return reject(err)
            if (result.length > 0) return resolve(result)
            if (result.length == 0) return resolve(result)
        })

    })

    p.then(r => {
        res.send({
            status: 200,
            mess: "成功",
            data: r
        })
    }, e => {
        res.send({
            status: 401,
            mess: e,
        })
    })
}

// 添加用户抽的奖
addUserAward = (req,res)=>{
    let data = req.query
    
    let sql = "insert into useraward set ? "
    let p = new Promise((resolve,reject)=>{
        db.query(sql,data,(err,result)=>{
            if(err) return reject(0)
            if(result.affectedRows>=0){
                resolve(1)
            }else{
                resolve(1)
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            mess:"成功",
            data:r
        })
    },e=>{
        res.send({
            status:400,
            mess:"失败",
            data:e
        })
    })


}

// 获取用户的奖品
getUserAward = (req,res)=>{
    let openId = req.query.openId
    let sql = "select * from useraward where openId = ? order by awardId desc" 

    let p = new Promise((resolve, reject) => {
        db.query(sql, [openId], (err, result) => {
            //   console.log(result)
            if (err) return reject(err)

            if (result.length > 0) {
                resolve(result)
            } else {
                resolve([])
            }
        })
    })
    p.then(r => {

        r.forEach(item => {
            item.awardDate = plugins.dateConversion(item.awardDate)
        })
        res.send({
            status: 200,
            data: r,
            mess: "获取成功"
        })
    }, e => {
        res.send({
            status: 400,
            data: [],
            mess: e
        })
    })
}

// 我草你妈的
// 用户领取之后删除奖品
deleteUserAward = (req,res)=>{
    let awardId = req.query.awardId
    let openId = req.query.openId
    let sql = "delete from useraward where awardId=? and openId=?"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,[awardId,openId],(err,result)=>{
            if(err) return reject(0)
            if(result.affectedRows>0){
                return resolve(1)
            }else{
                return reject(0)
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:r,
            mess:"删除成功"
        })
    },e=>{
        res.send({
            status:400,
            data:e,
            mess:"删除失败"
        })
    })

}

// 积分商品
integralShop = (req,res)=>{
    let data = req.query

    let sql = "insert into integralshop set ?"

    let p = new Promise((resolve,reject)=>{
        db.query(sql,data,(err,result)=>{
            if(err)return reject(0)
            if(result.affectedRows>0){
                return resolve(1)
            }else{
                return resolve(0)
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:r,
            mess:"成功"
        })
    },e=>{
        res.send({
            status:400,
            data:e,
            mess:"失败"
        })
    })
    
}

// 获取积分商品
getIntegralShop = (req,res)=>{
    let sql = "select * from  integralshop"
    
    let p = new Promise((resolve,reject)=>{
        db.query(sql,'',(err,result)=>{
            if(err)return reject()
            if(result.length>0){
                return resolve(result)
            }else{
                return resolve([])
            }
        })
    })
    p.then(r=>{
        res.send({
            status:200,
            data:r,
            mess:"成功"
        })
    },e=>{
        res.send({
            status:400,
            data:e,
            mess:"失败"
        })
    })
}

// 设置路由
setRouter=(req,res)=>{
    let sql = "insert into router set ?"
    let data = req.query

    let p = new Promise((resolve,reject)=>{
        db.query(sql,data,(err,result)=>{
            if(err) return reject("设置失败")
            if(result.affectedRows>0){
                return resolve("设置成功")
            }else{
                return reject("设置失败")
            }
        })
    })

    p.then(r=>{
        res.send({
            status:200,
            mess:r
        })
    },e=>{
        res.send({
            status:400,
            mess:e
        })
    })
    
},



module.exports = {
    pushShop,
    getShop,
    pushShoppingCart,
    getShoppingCartData,
    getShopTitle,
    getShopAddress,
    pushShopAddress,
    pushCartList,
    getCartList,
    setShopNum,
    deleteCartShop,
    clearCart,
    pushOrder,
    getOrderList,
    getOneOrder,
    changeOrderActive,
    pushShopTitle,
    addDiscount,
    getDiscount,
    userAddDiscount,
    getUserAddDiscount,
    changedisIstrue,
    deleteUserDiscount,
    getUerPayPassword,
    insertPaypassword,
    getUserMoney,
    matchPayPassword,
    getTransactionInfo,
    matchPassword,
    noMatchPassword,
    changePayPassword,
    editStoreAddress,
    deleteStoreAddress,
    getShopInfo,
    changeShop,
    deleteShop,
    deleteShopTitle,
    setShopTitle,
    cmsGetOrderList,
    receivedOrderList,
    insertLuckDraw,
    getLuckDraw,
    addUserAward,
    getUserAward,
    deleteUserAward,
    integralShop,
    getIntegralShop,
    setRouter
}